import {
    HttpRequest,
    HttpHandler,
    HttpEvent
} from '@angular/common/http';
import { Injectable, Inject, Injector } from '@angular/core';
import { HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { CommService } from 'app/ptpmc/comm/comm.service';
import { LocalStorage } from 'app/ptpmc/localstorage/local.storage';
import { Router, ActivatedRoute } from '@angular/router';
import { Notifycation } from 'app/ptpmc/comm/notifycation';

@Injectable()
export class JWTInterceptor implements HttpInterceptor {
    constructor(private inj: Injector, private ls: LocalStorage, private router: Router, ) {
    }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        const JWT = this.ls.get('ptpmc_token');

        const url = req.url;

        if (url === undefined) {
            return;
        }

        // if (JWT === undefined) {
        //     this.router.navigate(['']);
        //     Notifycation.showNotification('您当前没有权限，请先登陆', 'info');
        //     return;
        // }

        req = req.clone({
            setHeaders: {
                'Content-Type': 'application/json',
                'access-token': JWT
            }
        });
        return next.handle(req);
    }
}